package gen

import (
	"fmt"
	"log"
	"net/http"
)

type router struct {
	handlerMap map[string]HandlerFunc
}

func newRouter() *router {
	return &router{
		handlerMap: make(map[string]HandlerFunc),
	}
}

func (self *router) addRouter(method string, path string, handler HandlerFunc) {
	log.Printf("Route %s - %s", method, path)
	key := fmt.Sprintf(RouterMapKey, method, path)
	self.handlerMap[key] = handler
}

func (self *router) handler(c *Context) {
	key := fmt.Sprintf(RouterMapKey, c.Method, c.Path)
	if handle, ok := self.handlerMap[key]; ok {
		handle(c)
	} else {
		c.String(http.StatusNotFound, "404 Not Found: %s\n", c.Path)
	}
}
